package com.etooling.framework.controller;

import com.etooling.framework.entity.ChineseArea;
import com.etooling.framework.repository.ChineseAreaRepository;
import com.etooling.framework.svo.ChineseAreaOptionVo;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@EnableAutoConfiguration
/**
 * @title: DataDirController
 * @description:
 * @author 周志荣
 * @date 2020/12/19 12:11
 */
public class ChineseAreaController {
    @Autowired
    ChineseAreaRepository chineseAreaRepository;

    //异步获取数据
    @RequestMapping("/admin/getChineseAreaTree")
    @ResponseBody
    ReplyObj getAreaTree(
    ) {
        try {
            List<ChineseArea> children = chineseAreaRepository.findAll();
            Map<String, List<ChineseArea>> allChildrenNodes = this.getAllChildrenNode(children);
            List<ChineseArea> provinceList = chineseAreaRepository.getProvinceList();
            List<ChineseAreaOptionVo> treeList = new ArrayList<>();
            for (ChineseArea province : provinceList) {
                ChineseAreaOptionVo node = new ChineseAreaOptionVo();
                node.setValue(province.getName());
                node.setLabel(province.getName());
                node.setId(province.getId());
                node = this.getChildTreeNode(node, allChildrenNodes);
                treeList.add(node);
            }

            return WebUtils.flushSuccess(treeList);
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("数据异常");
        }
    }


    public Map<String, List<ChineseArea>> getAllChildrenNode(List<ChineseArea> allChildrenNode) {
        Map<String, List<ChineseArea>> allNodeMap = new HashMap<String, List<ChineseArea>>();
        for (int i = 0; i < allChildrenNode.size(); i++) {
            ChineseArea node = allChildrenNode.get(i);
            if (allNodeMap.get(node.getPid()) == null) {
                List<ChineseArea> subList = new ArrayList<ChineseArea>();
                subList.add(node);
                allNodeMap.put(node.getPid(), subList);
            } else {
                List<ChineseArea> subList = allNodeMap.get(node.getPid());
                subList.add(node);
            }
        }
        System.out.println(allNodeMap.get("1101").size());

        return allNodeMap;
    }

    public ChineseAreaOptionVo getChildTreeNode(ChineseAreaOptionVo rootTreeNode, Map<String, List<ChineseArea>> allChildrenNodeList) throws Exception {
        HashMap treeMap = new HashMap();
        List treelist = new ArrayList<>();
        String rootId = rootTreeNode.getId();
        List<ChineseArea> list = allChildrenNodeList.get(rootId);
        if (list != null && list.size() > 0) {
            for (ChineseArea node : list) {
                ChineseAreaOptionVo treeVo = new ChineseAreaOptionVo();
                treeVo.setValue(node.getName());
                treeVo.setId(node.getId());
                treeVo.setLabel(node.getName());
                treelist.add(treeVo);
                getChildTreeNode(treeVo, allChildrenNodeList);
            }
            rootTreeNode.setChildren(treelist);
        }
        return rootTreeNode;
    }

}


